﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.IO;
using Windvoice.Albums;
using Windvoice.Library.Graphic;

namespace Windvoice.Areas.Manage.Controllers
{
    public class PhotoController : ManagerControllerBase
    {
        //
        // GET: /Manage/Photo/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Upload()
        {
            ViewBag.AlbumList = Album.FindAll();
            ViewBag.SessionID = Session.SessionID;
            return View();
        }

        [HttpPost]
        public ActionResult Reciever(FormCollection formValues)
        {
            int AlbumID = Convert.ToInt32(Request.Form["album"]);
            int BatchID = Convert.ToInt32(Request.Form["batch"]);

            string result = "yes";
            
            foreach (var file in Request.Files.AllKeys)
            {
                //var file = Request.Files.AllKeys[0];
                string PhotoGuid = Guid.NewGuid().ToString();
                string RelativeOriginalPath = String.Format("/files/photo/{0}/{1}{2}", AlbumID, PhotoGuid, Path.GetExtension(Request.Files[file].FileName));
                string RelativeSmallPath = String.Format("/files/photo/{0}/{1}-s{2}", AlbumID, PhotoGuid, Path.GetExtension(Request.Files[file].FileName));

                string SavePath = Server.MapPath(RelativeOriginalPath);
                if (!new FileInfo(SavePath).Directory.Exists) new FileInfo(SavePath).Directory.Create();
                Request.Files[file].SaveAs(SavePath);
                result = ImageHelper.Thumbnail(SavePath, Server.MapPath(RelativeSmallPath), 180, 320, 100).ToString();

                Photo p = new Photo()
                {
                    AlbumID = AlbumID,
                    Path = RelativeOriginalPath,
                    SmallPath = RelativeSmallPath,
                    CreateTime = DateTime.Now,
                    Flag = BatchID,
                    Guid = PhotoGuid
                };
                p.Create();
                
                var album = Album.Find(AlbumID);
                if (album.FacePhoto == 0)
                {
                    album.FacePhoto = p.ID;
                    album.Update();
                }
            }
            return Content(result);
        }

        public ActionResult Edit(int id)
        {
            var photo = Photo.Find(id);
            if (photo != null)
                return View(photo);
            return View("404");
        }

        [HttpPost]
        public ActionResult Edit(Photo photo)
        {
            var old = Photo.Find(photo.ID);
            old.Description = photo.Description;
            if (ModelState.IsValid)
            {
                old.Save();
                return Redirect("/manage/album/detail/" + old.AlbumID);
            }
            return View(photo);
        }

        [HttpPost]
        public ActionResult Delete(int? id)
        {
            int photoID = id == null ? 0 : id.Value;
            if (photoID != 0)
            {
                Photo.Find(photoID).Delete();
                return AjaxResult();
            }

            return AjaxResult(result: false, message: "参数错误！");
        }

        [HttpPost]
        public ActionResult SetFacePhoto(FormCollection formValues)
        {
            int photoID = String.IsNullOrEmpty(formValues["photoid"]) ? 0 : Convert.ToInt32(formValues["photoid"]);

            if (photoID != 0)
            {
                var photo = Photo.Find(photoID);
                photo.SetFacePhoto();
                return AjaxResult();
            }

            return AjaxResult(result:false,message:"参数错误！");
        }
    }
}